/**
 * User: muxin4ever
 * Date: Oct, 31, 2012
 * Time: 21:25
 */

package handler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

import calculation.Matrix;
import calculation.MatrixLibrary;

public class OutputHandler {
	private MatrixLibrary matrixLibrary;
	private FileHandler fileHandler;
	private String subroutinePath = null;
	private String x = null;
	private String y = null;

	public OutputHandler(FileHandler fileHandler, MatrixLibrary matrixLibrary) {
		this.matrixLibrary = matrixLibrary;
		this.fileHandler = fileHandler;
	}

	public void matrixToFile(Matrix m, String filename) {

		for (int i = 0; i < m.getCol(); i++) {
			for (int j = 0; j < m.getRow(); j++) {

			}
		}
	}

	public void copyResultFile(String resultMatrixName) {
		fileHandler.copyFile(fileHandler.getHomePath() + File.separator
				+ "result.mtx", fileHandler.getLibraryDirectoryPath(),
				resultMatrixName);
	}

	public void writeCommandFile(String operation, String matrix1,
			String matrix2) {
		String abbrev = CalculationHandler.opName.get(operation);

		String commandFilePath = fileHandler.getHomePath() + File.separator
				+ "command.txt";

		File outputFile = new File(commandFilePath);

		PrintWriter out = null;

		try {
			out = new PrintWriter(outputFile);
			if (abbrev != null)
				out.println(abbrev);
			if (matrix1 != null)
				out.println(fileHandler.getLibraryDirectoryPath()
						+ File.separator + matrix1);
			if (matrix2 != null)
				out.println(fileHandler.getLibraryDirectoryPath()
						+ File.separator + matrix2);
			if(abbrev.equals("Z")){
				//dimension x
				out.println(this.getX());
				//dimension x
				out.println(this.getY());
			}
			out.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public boolean callFortran() {
		boolean status = false;
		String fortranPath = fileHandler.getHomePath() + File.separator
				+ "execute.exe";
		String[] cmdF = { fortranPath };
		try {
			Process p = Runtime.getRuntime().exec(cmdF);
			InputStream in = p.getInputStream();
			OutputStream out = p.getOutputStream();
			InputStream err = p.getErrorStream();

			Scanner sc = new Scanner(in);
			while (sc.hasNext()) {
				String line = sc.nextLine();
				System.out.println(line);
				if (line.contains("READY")) {
					status = true;
				}
			}

			p.waitFor();
		} catch (Exception e) {
			System.err.println("Call Fortran fail.");
		}
		return status;
	}

	public void updateSubroutine(String subroutinePath) {
		this.subroutinePath = subroutinePath;
	}

	public void setDimension(String x, String y) {
		this.x = x;
		this.y = y;
	}

	public String getX() {
		return this.x;
	}
	public String getY() {
		return this.y;
	}
}
